package com.liveneo.adapter.mobile.support;

import java.util.HashMap;
import java.util.Map;
import com.liveneo.adapter.mobile.model.BaseResponse;

/**
 * 移动HTTP请求的响应支持工具类
 * 
 * @author fucy
 *
 */
public class MobileReturnCodeSupport {
    // private static final Logger logger = Logger.getLogger(MobileReturnCodeSupport.class);
    // 手机请求的响应错误码
    public static final String              CODE_SUCCESS                       = "0";                          // 成功
    public static final String              CODE_NO_HTTP                       = "01";                         // 非HTTP请求错误
    public static final String              CODE_ERR_PARAM                     = "02";                         // 参数解析错误
    public static final String              CODE_ERR_VERSION                   = "03";                         // 错误的APP版本
    public static final String              CODE_ERR_AUTH                      = "04";                         // 用户认证失败
    public static final String              CODE_NOVALID_PARAM                 = "05";                         // 参数校验失败系统运行问题
    public static final String              CODE_INTERNAL_ERROR                = "06";                         // 内部错误
    // 手机响应的详细错误码
    public static final String              CODE_ERR_PARAM_NO_EXISTS           = "0200";                       // 不存在有效参数
    public static final String              CODE_ERR_PARAM_FAILED_DECRYPT      = "0201";                       // 参数解密失败
    public static final String              CODE_ERR_PARAM_FAILED_TO_OBJ       = "0202";                       // 参数json字符串转换成对象失败
    // 用户认证失败
    public static final String              CODE_ERROR_AUTH_PASSWORDERROR      = "0400";                       // 用户密码错误
    public static final String              CODE_ERROR_AUTH_PHONENO_NOTEXISTS  = "0401";                       // 手机号未找到用户
    public static final String              CODE_ERROR_AUTH_ALREADY_LOGIN      = "0402";                       // 该用户已经登陆
    public static final String              CODE_ERROR_AUTH_USERCODE_NOTEXISTS = "0403";                       // 用户代码未找到用户
    public static final String              CODE_ERROR_AUTH_LOSSUUID_SURVEY    = "0404";                       // 任务ID未找到对应的查勘信息
    // 参数校验失败
    public static final String              CODE_PARAM_NO_VERSIONCODE          = "0500";                       // 参数不存在版本号
    public static final String              CODE_PARAM_NO_VERSIONNAME          = "0501";                       // 参数不存在版本名
    public static final String              CODE_PARAM_NO_TOKEN                = "0502";                       // 参数不存在令牌
    public static final String              CODE_PARAM_NO_PHONENO              = "0503";                       // 参数不存在手机号
    public static final String              CODE_PARAM_NO_PASSWORD             = "0504";                       // 参数不存在密码
    public static final String              CODE_PARAM_NO_UUID                 = "0505";                       // 参数不存在UUID
    public static final String              CODE_PARAM_NO_USERCODE             = "0506";                       // 参数不存在用户代码
    public static final String              CODE_PARAM_NO_PHONETYPE            = "0507";                       // 参数不存在手机类型
    public static final String              CODE_PARAM_NO_PAGEINFO             = "0508";                       // 参数分页信息不能为空
    public static final String              CODE_PARAM_NO_ISFINISH             = "0509";                       // 参数不存在完成标识
    public static final String              CODE_PARAM_NO_CASEUUID             = "0510";                       // 参数不存在案件id
    public static final String              CODE_PARAM_NO_LOSSUUID             = "0511";                       // 参数不存在任务id
    public static final String              CODE_PARAM_NO_FLAG                 = "0512";                       // 参数不存在标识
    public static final String              CODE_PARAM_NO_OS                   = "0513";                       // 参数不存在终端类型
    public static final String              CODE_PARAM_NO_PHONEMODEL           = "0514";                       // 参数不存在手机型号
    public static final String              CODE_PARAM_NO_SYSTEMINFO           = "0515";                       // 参数不存在系统版本
    public static final String              CODE_PARAM_NO_SYSTEMCODE           = "0516";                       // 参数不存在系统代码
    public static final String              CODE_PARAM_NO_CASENO               = "0517";                       // 参数不存在案件编号
    public static final String              CODE_PARAM_NO_RECEIVETIME          = "0518";                       // 参数不存在接收时间
    public static final String              CODE_PARAM_NO_RECEIVENAME          = "0519";                       // 参数不存在接收人
    public static final String              CODE_PARAM_NO_OPERATORTYPE         = "0520";                       // 参数不存在操作类型
    public static final String              CODE_PARAM_NO_REASSIGNREASON       = "0521";                       // 参数不存在改派说明
    public static final String              CODE_PARAM_NO_REASSIGNPLAT         = "0522";                       // 参数不存在改派平台
    public static final String              CODE_PARAM_NO_REASSIGNTYPE         = "0523";                       // 参数不存在改派类型
    public static final String              CODE_PARAM_NO_LOSSNAME             = "0524";                       // 参数不存在损失名称
    public static final String              CODE_PARAM_NO_LOSSTYPE             = "0525";                       // 参数不存在损失类型
    public static final String              CODE_PARAM_NO_NOT                  = "0526";                       // 参数不存在损失类型
    public static final String              CODE_PARAM_NO_BANK                 = "0527";                       // 所属银行不存在
    public static final String              CODE_PARAM_NO_PROVICE              = "0528";                       // 所属银行不存在
    public static final String              CODE_PARAM_NO_CITY                 = "0529";                       // 所属银行不存在
    // 参数数据验证
    public static final String              CODE_LOSSUUID_NO_ERROR             = "0600";                       // 损失项lossuuid不存在
    public static final String              CODE_CASENO_NO_ERROR               = "0601";                       // 损失项caseNo不存在
    public static final String              SERVICE_CODE                       = "VideoSurveyService";         // 服务代码
    public static final String              SYS_CODE                           = "01";                         // 系统代码
    public static final String              CODE_FAIL                          = "9";
    public static final String              KEY_MOBILE_00000                   = "00000";
    public static final String              KEY_MOBILE_00001                   = "00001";
    public static final String              KEY_MOBILE_00002                   = "00002";
    public static final String              KEY_MOBILE_00003                   = "00003";
    public static final String              KEY_MOBILE_00004                   = "00004";
    public static final String              KEY_MOBILE_00005                   = "00005";
    public static final String              KEY_MOBILE_00006                   = "00006";
    public static final String              KEY_MOBILE_00007                   = "00007";
    public static final String              KEY_MOBILE_00008                   = "00008";
    public static final String              KEY_MOBILE_00009                   = "00009";
    public static final String              KEY_MOBILE_00010                   = "000010";
    public static final String              KEY_MOBILE_00011                   = "000011";
    public static final String              KEY_MOBILE_00012                   = "000012";
    public static final String              KEY_MOBILE_00013                   = "000013";
    public static final String              KEY_MOBILE_00014                   = "000014";
    public static final String              KEY_MOBILE_00015                   = "000015";
    public static final String              KEY_MOBILE_00016                   = "000016";
    public static final String              KEY_MOBILE_00017                   = "000017";
    public static final String              KEY_MOBILE_00018                   = "000018";
    public static final String              KEY_MOBILE_00019                   = "000019";
    public static final String              KEY_MOBILE_00020                   = "000020";
    public static final String              KEY_MOBILE_00021                   = "000021";
    public static final String              KEY_MOBILE_00022                   = "000022";
    public static final String              KEY_MOBILE_00023                   = "000023";
    public static final String              KEY_MOBILE_00024                   = "000024";
    public static final String              KEY_MOBILE_00025                   = "000025";
    public static final String              KEY_MOBILE_00026                   = "000026";
    public static final String              KEY_MOBILE_00027                   = "000027";
    public static final String              KEY_MOBILE_00028                   = "000028";
    public static final String              KEY_MOBILE_00029                   = "000029";
    public static final String              KEY_MOBILE_00030                   = "000030";
    public static final String              KEY_MOBILE_00031                   = "000031";
    public static final String              KEY_MOBILE_00032                   = "000032";
    public static final String              KEY_MOBILE_00033                   = "000033";
    public static final String              KEY_MOBILE_00034                   = "000034";
    public static final String              KEY_MOBILE_00035                   = "000035";
    public static final String              KEY_MOBILE_00036                   = "000036";
    public static final String              KEY_MOBILE_00037                   = "000037";
    public static final String              KEY_MOBILE_00038                   = "000038";
    public static final String              KEY_MOBILE_00039                   = "000039";
    /* zhangyw增加 */
    public static final String              KEY_MOBILE_00040                   = "000040";
    public static final String              KEY_MOBILE_00041                   = "000041";
    public static final String              KEY_MOBILE_00042                   = "000042";
    public static final String              KEY_MOBILE_00043                   = "000043";
    public static final String              KEY_MOBILE_00044                   = "000044";
    public static final String              KEY_MOBILE_00045                   = "000045";
    public static final String              KEY_MOBILE_00046                   = "000046";
    public static final String              KEY_MOBILE_00047                   = "000047";
    public static final String              KEY_MOBILE_00048                   = "000048";
    public static final String              KEY_MOBILE_00049                   = "000049";
    public static final String              KEY_MOBILE_00050                   = "000050";
    public static final String              KEY_MOBILE_00051                   = "000051";
    /* zhangyw结束 */
    // 参数解析错误码和错误信息Map
    public static final Map<String, String> parseParamCodeMap                  = new HashMap<String, String>();
    // 用户认证错误信息Map
    public static final Map<String, String> authParamCodeMap                   = new HashMap<String, String>();
    // 参数校验错误码和错误信息Map
    public static final Map<String, String> validParamCodeMap                  = new HashMap<String, String>();
    // 结果响应码Map
    public static final Map<String, Object> resultCodeMap                      = new HashMap<String, Object>();
    static {
        parseParamCodeMap.put(CODE_ERR_PARAM_NO_EXISTS, "不存在有效参数");
        parseParamCodeMap.put(CODE_ERR_PARAM_FAILED_DECRYPT, "参数解密失败");
        parseParamCodeMap.put(CODE_ERR_PARAM_FAILED_TO_OBJ, "参数json字符串转换成对象失败");
        authParamCodeMap.put(CODE_ERROR_AUTH_PASSWORDERROR, "用户密码错误");
        authParamCodeMap.put(CODE_ERROR_AUTH_PHONENO_NOTEXISTS, "手机号未找到用户");
        authParamCodeMap.put(CODE_ERROR_AUTH_ALREADY_LOGIN, "该用户已经登陆");
        authParamCodeMap.put(CODE_ERROR_AUTH_USERCODE_NOTEXISTS, "用户代码未找到用户");
        authParamCodeMap.put(CODE_ERROR_AUTH_LOSSUUID_SURVEY, "任务ID未找到对应的查勘信息");
        validParamCodeMap.put(CODE_PARAM_NO_VERSIONCODE, "参数不存在版本号");
        validParamCodeMap.put(CODE_PARAM_NO_VERSIONNAME, "参数不存在版本名");
        validParamCodeMap.put(CODE_PARAM_NO_TOKEN, "参数不存在令牌");
        validParamCodeMap.put(CODE_PARAM_NO_PHONENO, "参数不存在手机号");
        validParamCodeMap.put(CODE_PARAM_NO_PASSWORD, "参数不存在密码");
        validParamCodeMap.put(CODE_PARAM_NO_UUID, "参数不存在UUID");
        validParamCodeMap.put(CODE_PARAM_NO_USERCODE, "参数不存在用户代码");
        validParamCodeMap.put(CODE_PARAM_NO_PHONETYPE, "参数不存在终端类型");
        validParamCodeMap.put(CODE_PARAM_NO_PAGEINFO, "分页参数不能为空");
        validParamCodeMap.put(CODE_PARAM_NO_ISFINISH, "参数不存在完成标识");
        validParamCodeMap.put(CODE_PARAM_NO_CASEUUID, "参数不存在案件id");
        validParamCodeMap.put(CODE_PARAM_NO_LOSSUUID, "参数不存在任务id");
        validParamCodeMap.put(CODE_PARAM_NO_FLAG, "参数不存在标识");
        validParamCodeMap.put(CODE_PARAM_NO_OS, "参数不存在终端类型");
        validParamCodeMap.put(CODE_PARAM_NO_PHONEMODEL, "参数不存在手机型号");
        validParamCodeMap.put(CODE_PARAM_NO_SYSTEMINFO, "参数不存在系统版本");
        validParamCodeMap.put(CODE_PARAM_NO_SYSTEMCODE, "参数不存在系统代码");
        validParamCodeMap.put(CODE_PARAM_NO_CASENO, "参数不存在案件编号");
        validParamCodeMap.put(CODE_PARAM_NO_RECEIVETIME, "参数不存在接收时间");
        validParamCodeMap.put(CODE_PARAM_NO_RECEIVENAME, "参数不存在接收人");
        validParamCodeMap.put(CODE_PARAM_NO_OPERATORTYPE, "参数不存在操作类型");
        validParamCodeMap.put(CODE_PARAM_NO_REASSIGNREASON, "参数不存在改派说明");
        validParamCodeMap.put(CODE_PARAM_NO_REASSIGNPLAT, "参数不存在改派平台");
        validParamCodeMap.put(CODE_PARAM_NO_REASSIGNTYPE, "参数不存在改派类型");
        validParamCodeMap.put(CODE_PARAM_NO_LOSSNAME, "参数不存在损失名称");
        validParamCodeMap.put(CODE_PARAM_NO_LOSSTYPE, "参数不存在损失类型");
        validParamCodeMap.put(CODE_PARAM_NO_NOT, "参数为空或不完整，请检查参数");
        validParamCodeMap.put(CODE_PARAM_NO_BANK, "所属银行不存在");
        validParamCodeMap.put(CODE_PARAM_NO_PROVICE, "所属省份不存在");
        validParamCodeMap.put(CODE_PARAM_NO_CITY, "所属城市不存在");
        resultCodeMap.put(CODE_SUCCESS, "成功");
        resultCodeMap.put(CODE_NO_HTTP, "非HTTP请求错误");
        resultCodeMap.put(CODE_ERR_PARAM, parseParamCodeMap);
        resultCodeMap.put(CODE_ERR_VERSION, "错误的APP版本");
        resultCodeMap.put(CODE_ERR_AUTH, authParamCodeMap);
        resultCodeMap.put(CODE_NOVALID_PARAM, validParamCodeMap);
        resultCodeMap.put(CODE_INTERNAL_ERROR, "系统内部错误");
        resultCodeMap.put(CODE_LOSSUUID_NO_ERROR, "损失项lossuuid不存在或错误");
        resultCodeMap.put(CODE_CASENO_NO_ERROR, "损失项caseNo不存在或错误");
        resultCodeMap.put(CODE_FAIL, "失败");
        resultCodeMap.put(KEY_MOBILE_00000, "系统操作出现异常");
        resultCodeMap.put(KEY_MOBILE_00001, "用户名为空");
        resultCodeMap.put(KEY_MOBILE_00002, "密码为空");
        resultCodeMap.put(KEY_MOBILE_00003, "用户名不存在");
        resultCodeMap.put(KEY_MOBILE_00004, "密码不正确");
        resultCodeMap.put(KEY_MOBILE_00005, "该账号无效");
        resultCodeMap.put(KEY_MOBILE_00006, "发送手机验证码失败");
        resultCodeMap.put(KEY_MOBILE_00007, "登陆失败");
        resultCodeMap.put(KEY_MOBILE_00008, "修改密码失败");
        resultCodeMap.put(KEY_MOBILE_00009, "申请改派失败");
        resultCodeMap.put(KEY_MOBILE_00010, "上传图片没有数据");
        resultCodeMap.put(KEY_MOBILE_00011, "请求理赔失败");
        resultCodeMap.put(KEY_MOBILE_00012, "案件正在处理中，请稍候！");
        resultCodeMap.put(KEY_MOBILE_00013, "获取最新版本失败");
        resultCodeMap.put(KEY_MOBILE_00014, "当前为最新版本");
        resultCodeMap.put(KEY_MOBILE_00015, "有新版本");
        resultCodeMap.put(KEY_MOBILE_00016, "没有案件");
        resultCodeMap.put(KEY_MOBILE_00017, "案件转人工失败");
        resultCodeMap.put(KEY_MOBILE_00018, "手机号为空");
        resultCodeMap.put(KEY_MOBILE_00019, "与理赔交互失败");
        resultCodeMap.put(KEY_MOBILE_00020, "更新确认状态失败");
        resultCodeMap.put(KEY_MOBILE_00021, "查询定损单状态失败");
        resultCodeMap.put(KEY_MOBILE_00022, "没有该变量");
        resultCodeMap.put(KEY_MOBILE_00023, "查勘完成异常");
        resultCodeMap.put(KEY_MOBILE_00024, "当前用户已在其他设备登陆！");
        resultCodeMap.put(KEY_MOBILE_00025, "没有对应的公司！");
        resultCodeMap.put(KEY_MOBILE_00026, "获取案件排队数和预测时长失败！");
        resultCodeMap.put(KEY_MOBILE_00027, "没有该案件！");
        resultCodeMap.put(KEY_MOBILE_00028, "获取案件状态失败！");
        resultCodeMap.put(KEY_MOBILE_00029, "没有需要处理的案件！");
        resultCodeMap.put(KEY_MOBILE_00030, "获取案件排队情况接口失败！");
        resultCodeMap.put(KEY_MOBILE_00031, "手机注册解绑失败！");
        resultCodeMap.put(KEY_MOBILE_00032, "操作标识错误！");
        resultCodeMap.put(KEY_MOBILE_00033, "信息列表为空！");
        resultCodeMap.put(KEY_MOBILE_00034, "定损单还未生成！");
        resultCodeMap.put(KEY_MOBILE_00035, "双向留言失败！");
        resultCodeMap.put(KEY_MOBILE_00036, "系统不支持该上传文件类型！");
        resultCodeMap.put(KEY_MOBILE_00037, "查询手机地址版本信息");
        resultCodeMap.put(KEY_MOBILE_00038, "此单证任务正在处理中");
        resultCodeMap.put(KEY_MOBILE_00039, "此任务不存在或者已经上传完成");
        /* zhangyw增加 */
        /* MP25接口用 */
        resultCodeMap.put(KEY_MOBILE_00040, "提交的参数不符合检测新版本的条件");
        resultCodeMap.put(KEY_MOBILE_00041, "数据库中查不到对应的版本信息！");
        /* MP31接口用 */
        resultCodeMap.put(KEY_MOBILE_00042, "提交的参数无效致使操作失败");
        /* MP61接口用 */
        resultCodeMap.put(KEY_MOBILE_00043, "必填项缺失");
        /* zhangyw增加结束 */
        resultCodeMap.put(KEY_MOBILE_00051, "没有对应的损失项信息");
    }

    /**
     * 构建一个基本的响应对象
     * 
     * @param code
     * @param detailCode
     * @return
     */
    /**
    public static BaseResponse buildBaseReponse(String code, String detailCode) {
        return buildBaseReponse(code, detailCode, null);
    }
    */
    /**
     * 
     * @Title：构建一个基本的响应对象(适用于根据code取出的是Map)
     * @author zhangyw 2016年10月20日 下午3:16:42
     * @param code
     * @param sysCode
     * @param detailCode
     * @return
     */
    public static BaseResponse buildBaseReponse(String code, String sysCode, String detailCode) {
        BaseResponse response = new BaseResponse();
        response.setResultCode(code);
        response.setSystemCode(sysCode);
        response.setExtMessage(getDetailMessage(code, detailCode));
        return response;
    }

    /**
     * 构建一个基本的响应对象(适用于直接赋错误信息)
     * @param code
     * @param sysCode
     * @param extMessage
     * @return
     */
    public static BaseResponse buildBaseReponse2(String code, String sysCode, String extMessage) {
        BaseResponse response = new BaseResponse();
        response.setResultCode(code);
        response.setSystemCode(sysCode);
        response.setExtMessage(extMessage);
        return response;
    }

    /**
     * 构建一个基本的响应对象(适用于根据code获取String，直接解析detailCode)
     * @param code
     * @param sysCode
     * @param detailCode
     * @return
     */
    public static BaseResponse buildBaseReponse3(String code, String sysCode, String detailCode) {
        BaseResponse response = new BaseResponse();
        response.setResultCode(code);
        response.setSystemCode(sysCode);
        response.setExtMessage(getDetailMessage2(code, detailCode));
        return response;
    }

    /**
     * 根据错误码获取详细错误信息(适用于code取出的是Map类型)
     * 
     * @param code 错误码（一般两位）
     * @param detailCode 详细错误码（一般四位）
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String getDetailMessage(String code, String detailCode) {
        String detailMessage = "无对应错误信息";
        // 获取错误码对应对象
        Object obj = resultCodeMap.get(code);
        if (obj != null) {
            if (obj instanceof Map) {
                // 错误码对应对象如果为Map类型，需要通过detailCode获取详细信息
                Map<String, String> detailMap = (Map<String, String>) obj;
                detailMessage = detailMap.get(detailCode);
            } else {
                // 错误码对应对象如果为字符串，说明详细信息为该字符串
                detailMessage = obj.toString();
            }
        }
        return detailMessage;
    }

    /**
     * 根据错误码获取详细错误信息(适用于detailCode取出的是String类型)
     * @param code
     * @param detailCode
     * @return
     */
    public static String getDetailMessage2(String code, String detailCode) {
        String detailMessage = "无对应中文解释信息";
        Object obj = resultCodeMap.get(detailCode);
        if (obj != null) {
            detailMessage = obj.toString();
        } else {
            detailMessage = resultCodeMap.get(code) != null ? resultCodeMap.get(code).toString() : detailMessage;
        }
        return detailMessage;
    }

    /**
     * 构建返回信息(不返回系统编码)
     * @param code
     * @param detailCode
     * @param clazz
     * @return
     */
    /**
     * 
     
    @SuppressWarnings("unchecked")
    public static <T extends BaseResponse> T buildReponse(String code, String detailCode, Class<T> clazz) {
        T response = null;
        if (clazz != null) {
            try {
                response = clazz.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            response = (T) new BaseResponse();
        }
        response.setResultCode(code);
        response.setExtMessage(getDetailMessage(code, detailCode));
        return response;
    }
    */
    /**
     * 构建返回信息(包含系统编码)
     * @param code
     * @param sysCode
     * @param detailCode
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends BaseResponse> T buildReponse(String code, String sysCode, String detailCode, Class<T> clazz) {
        T response = null;
        if (clazz != null) {
            try {
                response = clazz.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            response = (T) new BaseResponse();
        }
        response.setResultCode(code);
        response.setSystemCode(sysCode);
        response.setExtMessage(getDetailMessage(code, detailCode));
        return response;
    }

    /**
     * 构建返回信息(包含系统编码,适用于只有一个返回码，但又要求详细信息)
     * @param code
     * @param sysCode
     * @param extMessage
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends BaseResponse> T buildReponse2(String code, String sysCode, String extMessage, Class<T> clazz) {
        T response = null;
        if (clazz != null) {
            try {
                response = clazz.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            response = (T) new BaseResponse();
        }
        response.setResultCode(code);
        response.setSystemCode(sysCode);
        response.setExtMessage(extMessage);
        return response;
    }

    /**
     * 构建返回信息(包含系统编码,适用于根据code取不出Map，直接用detailCode取出详细信息)
     * @param code
     * @param sysCode
     * @param detailCode
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends BaseResponse> T buildReponse3(String code, String sysCode, String detailCode, Class<T> clazz) {
        T response = null;
        if (clazz != null) {
            try {
                response = clazz.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            response = (T) new BaseResponse();
        }
        response.setResultCode(code);
        response.setSystemCode(sysCode);
        response.setExtMessage(getDetailMessage2(code, detailCode));
        return response;
    }
}
